package com.maozzi.api.mq.config;

import com.maozzi.api.mq.constant.MqConstant;
import com.maozzi.api.mq.properties.MqProperties;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.StringUtils;

/**
 * 隔离配置
 *
 * @author maozi
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IsolationConfig implements BeanPostProcessor {
    private final MqProperties mqProperties;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof DefaultRocketMQListenerContainer container) {
            if (mqProperties.isEnableIsolation() && StringUtils.hasText(mqProperties.getSuffix())) {
                container.setTopic(container.getTopic() + MqConstant.ISOLATION_SEPARATOR + mqProperties.getSuffix());
            }
            return container;
        }
        return bean;
    }
}
